package com.gis.servlet;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gis.util.Constant;
import com.gis.util.ResponseUtil;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;

@WebServlet("/image")
public class ImageGetServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String fileName = new String(req.getParameter("fileName").getBytes("ISO-8859-1"),"utf-8");
		String filePath = Constant.UPLOAD_DIRECTORY + File.separator + fileName;
		if(StrUtil.isBlank(fileName) || !FileUtil.exist(filePath)) {
			filePath = Constant.UPLOAD_DIRECTORY + File.separator + "noimage001.jpg";
		}
		byte[] bytes = FileUtil.readBytes(filePath);
		ResponseUtil.writeImage(resp, bytes);
	}
	

}
